{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "db18bedd-8db8-4062-8dcc-59c1403566bd",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "# 列表索引和切片\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f0f3db06-9ae7-4ead-a165-1ac5a7c4a923",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表长度为\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "# 创建一个混合列表\n",
    "my_list = [1, 1.0, '1', True, [1, 1.0, '1'], {1}, {1:1.0}]\n",
    "print('列表长度为')\n",
    "print(len(my_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c5df9a54-37e5-4a1e-9587-67d166e1c930",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "元素：1，索引：0，类型：<class 'int'>\n",
      "元素：1.0，索引：1，类型：<class 'float'>\n",
      "元素：1，索引：2，类型：<class 'str'>\n",
      "元素：True，索引：3，类型：<class 'bool'>\n",
      "元素：[1, 1.0, '1']，索引：4，类型：<class 'list'>\n",
      "元素：{1}，索引：5，类型：<class 'set'>\n",
      "元素：{1: 1.0}，索引：6，类型：<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 打印每个元素和对应的序号\n",
    "for index, item in enumerate(my_list):\n",
    "    type_i = type(item)\n",
    "    print(f\"元素：{item}，索引：{index}，类型：{type_i}\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f30c0a54-2a6e-487f-9eeb-f1960319215d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "# 列表索引\n",
    "print(my_list[0])\n",
    "print(my_list[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f4099cb4-4abd-496f-aa31-fc129de19f4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 1.0}\n",
      "{1}\n"
     ]
    }
   ],
   "source": [
    "print(my_list[-1])\n",
    "print(my_list[-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8b5e0a4d-fc37-47a2-9591-62f45bac8293",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, '1']\n"
     ]
    }
   ],
   "source": [
    "# 列表切片\n",
    "# 取出前3个元素，索引为0、1、2\n",
    "print(my_list[:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8050c8f9-9315-4f1b-b905-6d3a4ef34ffb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.0, '1', True]\n"
     ]
    }
   ],
   "source": [
    "# 取出索引1、2、3，不含0，不含4\n",
    "print(my_list[1:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a2422ad1-2f37-4439-9e1a-712d64bd47af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, '1', [1, 1.0, '1'], {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 指定步长2，取出第0、2、4、6\n",
    "print(my_list[::2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7fef69ef-6699-47fc-9956-e543fbf4f241",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{1: 1.0}, {1}, [1, 1.0, '1'], True, '1', 1.0, 1]\n"
     ]
    }
   ],
   "source": [
    "# 指定步长-1，倒序\n",
    "print(my_list[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bf30e921-69f6-436e-9ffe-3ac0f90044c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "source": [
    "# 提取列表中的列表某个元素\n",
    "print(my_list[4][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c8bd7cd-340e-4b3c-9e8d-51831fb10164",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 切片和读写\n",
    "custom_list = [1,2,3,4,5,6,7,8,9,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d7101ee6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 错误的写法\n",
    "part_of_custom_list = custom_list[-2:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "0851c0bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "index = 1\n",
    "left = index - 2\n",
    "if left < 0:\n",
    "    left = 0\n",
    "right = index + 2\n",
    "if right > len(custom_list):\n",
    "    right = len(custom_list)\n",
    "part_of_custom_list = custom_list[left:right]\n",
    "print(part_of_custom_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "44eaa55c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 0, 3]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part_of_custom_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa379ed2",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list assignment index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[36], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m item,index \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(part_of_custom_list):\n\u001b[0;32m----> 2\u001b[0m     \u001b[43mpart_of_custom_list\u001b[49m\u001b[43m[\u001b[49m\u001b[43mindex\u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(part_of_custom_list)\n\u001b[1;32m      4\u001b[0m \u001b[38;5;28mprint\u001b[39m(custom_list)\n",
      "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range"
     ]
    }
   ],
   "source": [
    "for item,index in enumerate(part_of_custom_list):\n",
    "    part_of_custom_list[index] = 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73791bb1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 3]\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]\n"
     ]
    }
   ],
   "source": [
    "print(part_of_custom_list)\n",
    "# 切片的结果就是新的List 而不是旧的\n",
    "print(custom_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0db54318",
   "metadata": {},
   "outputs": [],
   "source": [
    "for"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "science39",
   "language": "python",
   "name": "science39"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
